package com.smyh.UIM.device;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.smyh.UIM.MainActivity;
import com.smyh.UIM.R;
import com.smyh.bean.DeviceParam4;
import com.smyh.cache.DeviceParamInfoCache;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.commcache.commd.UIParamOptionUtil;
import com.smyh.common.MinaClientThread;
import com.smyh.protcol.device.CMDUtil;
import com.smyh.util.HexStrUtil;

import org.apache.mina.core.buffer.IoBuffer;

/**
 * Created by Administrator on 2016/7/27.
 */
public class DeviceParam04Fm extends Fragment implements View.OnClickListener {
    private Button param4ReadBtn;
    private static TextView versionNum,runTimes,workStyle,reserv1,reserv2,reserv3,reserv4,reserv5,reserv6,reserv7,reserv8;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_device_param04,container,false);
        initView(view);
        initEvent();
        return view;
    }

    private void initEvent() {
        versionNum.setOnClickListener(this);
        runTimes.setOnClickListener(this);
        workStyle.setOnClickListener(this);
        reserv1.setOnClickListener(this);
        reserv2.setOnClickListener(this);
        reserv3.setOnClickListener(this);
        reserv4.setOnClickListener(this);
        reserv5.setOnClickListener(this);
        reserv6.setOnClickListener(this);
        reserv7.setOnClickListener(this);
        reserv8.setOnClickListener(this);
        param4ReadBtn.setOnClickListener(btnClickListenrer);
    }
    private View.OnClickListener btnClickListenrer = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.param4ReadBtn:{
                    if(MinaClientThread.session == null){
                        Toast.makeText(getContext(),"请连接wifi！",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    IoBuffer buf = CMDUtil.askOutParam(HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr), (byte) 4);
                    MinaClientThread.session.write(buf);
                    Toast.makeText(getContext(),"读取数据！",Toast.LENGTH_SHORT).show();
                    break;
                }
                default:{
                    break;
                }
            }
        }
    };

    private void initView(View view) {
        versionNum = (TextView) view.findViewById(R.id.version_num_1);
        runTimes = (TextView) view.findViewById(R.id.runTimes_2);
        workStyle = (TextView) view.findViewById(R.id.workStyle_3);
        reserv1 = (TextView) view.findViewById(R.id.reserv1);
        reserv2 = (TextView) view.findViewById(R.id.reserv2);
        reserv3 = (TextView) view.findViewById(R.id.reserv3);
        reserv4 = (TextView) view.findViewById(R.id.reserv4);
        reserv5 = (TextView) view.findViewById(R.id.reserv5);
        reserv6 = (TextView) view.findViewById(R.id.reserv6);
        reserv7 = (TextView) view.findViewById(R.id.reserv7);
        reserv8 = (TextView) view.findViewById(R.id.reserv8);

        param4ReadBtn = (Button) view.findViewById(R.id.param4ReadBtn);
    }
    static Handler handler1 = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            DeviceParam4 obj = DeviceParamInfoCache.getDeviceParamMap4().get(MainCahce.chooseDeviceAddr);
            if (obj != null){
                versionNum.setText(obj.getVersionNum()+"");
                runTimes.setText(obj.getRunTimes()+"");
                workStyle.setText(obj.getWorkStyle()+"");
                reserv1.setText(obj.getReserv1()+"");
                reserv2.setText(obj.getReserv2()+"");
                reserv3.setText(obj.getReserv3()+"");
                reserv4.setText(obj.getReserv4()+"");
                reserv5.setText(obj.getReserv5()+"");
                reserv6.setText(obj.getReserv6()+"");
                reserv7.setText(obj.getReserv7()+"");
                reserv8.setText(obj.getReserv8()+"");

            }else {
                versionNum.setText( " ");
                runTimes.setText( " ");
                workStyle.setText( " ");
                reserv1.setText( " ");
                reserv2.setText( " ");
                reserv3.setText( " ");
                reserv4.setText( " ");
                reserv5.setText( " ");
                reserv6.setText( " ");
                reserv7.setText( " ");
                reserv8.setText( " ");

            }
        }
    };
    public static void freshDeviceParam4Model() {
        handler1.sendMessage(new Message());
    }
    int index = 1;
    String str = "";
    @Override
    public void onClick(View v) {
        if(MinaClientThread.session==null){
            Toast.makeText(getContext(), "请连接wifi", Toast.LENGTH_SHORT).show();
            return;
        }
        if(MainCahce.chooseDeviceAddr.equals("")){
            Toast.makeText(getContext(), "请选择设备", Toast.LENGTH_SHORT).show();
            return;
        }
        switch (v.getId()){
            case R.id.version_num_1:{
                index = 1;
                str = versionNum.getText().toString();
                break;
            }
            case R.id.runTimes_2:{
                index = 2;
                str = runTimes.getText().toString();
                break;
            }
            case R.id.workStyle_3:{
                index = 3;
                str = workStyle.getText().toString();
                break;
            }
            case R.id.reserv1:{
                index = 4;
                str = reserv1.getText().toString();
                break;
            }case R.id.reserv2:{
                index = 5;
                str = reserv2.getText().toString();
                break;
            }case R.id.reserv3:{
                index = 6;
                str = reserv3.getText().toString();
                break;
            }case R.id.reserv4:{
                index = 7;
                str = reserv4.getText().toString();
                break;
            }case R.id.reserv5:{
                index = 8;
                str = reserv5.getText().toString();
                break;
            }
            case R.id.reserv6:{
                index = 9;
                str = reserv6.getText().toString();
                break;
            }
            case R.id.reserv7:{
                index = 10;
                str = reserv7.getText().toString();
                break;
            }case R.id.reserv8:{
                index = 11;
                str = reserv8.getText().toString();
                break;
            }
        }
        final EditText edit = new EditText(getContext());
        edit.setText(str);
        edit.setSelection(str.length());
        new AlertDialog.Builder(getContext()).setTitle("请输入").setView(edit)
                .setPositiveButton("修改", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            byte[] addrs = HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr);
                            IoBuffer buffer = UIParamOptionUtil.writeDeviceParm(addrs,4,index,Integer.valueOf(edit.getText().toString()));
                            MinaClientThread.session.write(buffer);
                        }catch (Exception e){
                            Toast.makeText(getContext(), "格式不正确", Toast.LENGTH_SHORT).show();
                        }
                    }
                }) .setNegativeButton("取消", null).show();
    }
}
